﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace webClient
{
    public class CustomClient
    {

        private HttpClient httpClient = new HttpClient();

        private string _url;
        public CustomClient(string url)
        {
            _url = url;
            httpClient.Timeout = TimeSpan.FromSeconds(2);
        }

        public async Task PostMsg(string msg)
        {
            var values = new[] { new KeyValuePair<string, string>("data", msg) };
            var content = new FormUrlEncodedContent(values);
            try
            {
                var response = await httpClient.PostAsync(_url, content);
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Data posted successfully!");
                    string responseData = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseData);
                }
                else
                {
                    Console.WriteLine($"Failed to post data. Status code: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine($"Exception occurred: {ex.Message}");
            }

        }
    }
}
